package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Course;
import com.xmy.cultivate.entity.CourseType;
import com.xmy.cultivate.entity.Dict;
import com.xmy.cultivate.entity.request.SubjectsClassTypeRs;
import com.xmy.cultivate.service.ICourseService;
import com.xmy.cultivate.service.ICourseTypeService;
import com.xmy.cultivate.service.IDictService;
import com.xmy.cultivate.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程类别 前端控制器
 * </p>
 *
 * @author min
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/api/courseType")
@Scope("prototype")
public class CourseTypeController extends BaseController {

    @Autowired
    ICourseTypeService iCourseTypeService;

    @Autowired
    IDictService iDictService;

    @Autowired
    ICourseService iCourseService;

    @Autowired
    IdWorker idWorker;

    @GetMapping()
    public Result findAll(@RequestParam Map reMap) {
        try {
            Integer pageNum = StringUtils.isEmpty((String) reMap.get("pageNum")) ? 1 : Integer.valueOf((String) reMap.get("pageNum"));
            Integer pageSize = StringUtils.isEmpty((String) reMap.get("pageSize")) ? 10 : Integer.valueOf((String) reMap.get("pageSize"));
            IPage<CourseType> iPage = new Page<>(pageNum, pageSize);
            QueryWrapper<CourseType> courseTypeQueryWrapper = new QueryWrapper<>();
            courseTypeQueryWrapper.orderByAsc("sort");

            iCourseTypeService.page(iPage, courseTypeQueryWrapper);
            return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @GetMapping("/allPaginateNot")
    public Result allPaginateNot() {
        QueryWrapper<CourseType> courseTypeQueryWrapper = new QueryWrapper<>();
        courseTypeQueryWrapper.orderByAsc("sort");
        List<CourseType> courseTypes = iCourseTypeService.list(courseTypeQueryWrapper);
        return new Result(Code.OK, courseTypes, Code.GET_OK_MSG);

    }

    @GetMapping("/findForQuarter")
    public Result findForQuarter(@RequestParam("courseId") String courseId) {
        try {
            Course course = iCourseService.getById(courseId);
            //1假期，0学期
            Integer isVacation = iDictService.getIsVacation(course.getQuarterNum());
            QueryWrapper<CourseType> courseTypeQueryWrapper = new QueryWrapper<>();
            if (isVacation.equals(1)) {
                courseTypeQueryWrapper.eq("type", 0).or().eq("type", 1);//调整为不分假期由2改为1
            } else {
                courseTypeQueryWrapper.eq("type", 0).or().eq("type", 1);
            }
            courseTypeQueryWrapper.orderByAsc("sort");

            List<CourseType> courseTypes = iCourseTypeService.list(courseTypeQueryWrapper);
            return new Result(Code.OK, new PageResult(courseTypes, (long) courseTypes.size(), 1L));
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @GetMapping("/getCourseTypeForQuarter")
    public Result getCourseTypeForQuarter(@RequestParam(value = "yearPart", required = false) String yearPart,
                                          @RequestParam(value = "quarterNum", required = false, defaultValue = "0") Integer quarterNum) {
        try {
            //1假期，0学期

            QueryWrapper<CourseType> courseTypeQueryWrapper = new QueryWrapper<>();
            if (quarterNum != 0) {
                Integer isVacation = iDictService.getIsVacation(quarterNum);
                if (isVacation.equals(1)) {
                    courseTypeQueryWrapper.eq("type", 0).or().eq("type", 1);//调整为都是获取学期班的班型
                } else {
                    courseTypeQueryWrapper.eq("type", 0).or().eq("type", 1);
                }
            }
            courseTypeQueryWrapper.orderByAsc("sort");
            List<CourseType> courseTypes = iCourseTypeService.list(courseTypeQueryWrapper);
            return new Result(Code.OK, new PageResult(courseTypes, (long) courseTypes.size(), 1L));
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }


    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id") String id) {
        try {
            CourseType courseType = iCourseTypeService.getById(id);
            //long ids =  idWorker.nextId();
            //System.out.println(ids);
            return new Result(Code.OK, courseType);
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }


    @PostMapping()
    public Result save(@RequestBody CourseType courseType) {
        try {

            boolean rs = iCourseTypeService.save(courseType);
            return new Result(rs ? Code.OK : Code.ERROR);
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable(value = "id") String id, @RequestBody CourseType courseType) {
        try {
            //System.out.println(courseType);
            //courseType.setUpdatedAt(DateUtil.getDateTime());
            boolean rs = iCourseTypeService.updateById(courseType);
            return new Result(rs ? Code.OK : Code.ERROR);
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") String id) {
        try {
            boolean rs = iCourseTypeService.removeById(id);
            return new Result(rs ? Code.OK : Code.ERROR);
        } catch (Exception e) {
            return new Result(Code.ERROR, e.getMessage());
        }
    }

    @GetMapping("/getSubjectsCourseTypeForGradeList")
    public Result getSubjectsCourseTypeForGradeList(@RequestParam(value = "yearPart", required = false,defaultValue = "0") Integer yearPart,
                                                    @RequestParam(value = "quarterNum", required = false, defaultValue = "0") Integer quarterNum){
        List<SubjectsClassTypeRs> courseTypes = iCourseTypeService.getSubjectsCourseTypeForGradeList(yearPart,quarterNum);
        return new Result(Code.OK, courseTypes, "");
    }
}
